import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:provider/provider.dart';
import 'package:xvideos/assets_tool/images.dart';
import 'package:xvideos/hj_page/main_page/provider/ai_bubble_provider.dart';
import 'package:xvideos/hj_page/main_page/provider/bottom_bar_provider.dart';
import 'package:xvideos/hj_page/pre_sale/pre_sale_provider.dart';
import 'package:xvideos/hj_page/short_video/short_video_logic.dart';
import 'package:xvideos/hj_utils/holiday_theme.dart';
import 'package:xvideos/hj_utils/screen.dart';
import 'package:xvideos/tools_base/global_store/store.dart';

import '../../../tools_base/event_bus/event_bus_util.dart';
import '../../../tools_base/event_bus/events.dart';
import '../main_page_logic.dart';
import '../main_page_state.dart';
import '../provider/msg_provider.dart';

//tabbar
class VideoMainBottomView extends StatefulWidget {
  const VideoMainBottomView({super.key});

  @override
  State<StatefulWidget> createState() {
    return _VideoMainBottomViewState();
  }
}

class _VideoMainBottomViewState extends State<VideoMainBottomView> {
  final MainPageLogic logic = Get.find<MainPageLogic>();

  MainPageState get state => logic.state;

  @override
  void initState() {
    super.initState();

    state.pageController.addListener(() {
      if (mounted) setState(() {});
    });
  }

  void _selectIndex(int index) {
    logic.jumpToPage(index);
    eventBus.emit(TabIndexChanged(index: index));
    presaleProvider.refreshPreSaleConfig(); //刷新预售状态
    MineMsgProvider().getMessageTip(); //刷新小红点
    if (index == 3) {
      ShortVideoLogic.play();
    } else {
      ShortVideoLogic.pauseAll();
    }
    //首页
    if (index == 0) {
      context.read<AIBubbleProvider>().changeRandom();
    }
    //我的
    if (index == state.titleArr.length - 1) {
      context.read<AIBubbleProvider>().changeRandom();
      globalStore.refreshWallet(); //刷新钱包
      globalStore.updateUserInfo(); //刷新个人信息
      presaleProvider.refreshPreSalStatus(); //刷新预售权益
    }
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.fromLTRB(12, 0, 12, screen.paddingBottom),
      decoration: BoxDecoration(
        color: Colors.black,
        border: Border(top: BorderSide(color: Colors.black.withOpacity(0.02))),
      ),
      child: Row(
        children: List.generate(state.imageName.length, (index) {
          if (index == 0) return _consumerItem<HomeBottomProvider>(index);
          if (index == 1) return _consumerItem<DarkwebBottomProvider>(index);
          if (index == 2) return _consumerItem<CommunityBottomProvider>(index);

          return _buildNormalItem(index);
        }),
      ),
    );
  }

  Widget _consumerItem<T extends BaseBottomProvider>(int index) {
    bool isSelected = index == state.curIndex;

    String titleDesc = state.titleArr[index];

    return Consumer<T>(builder: (_, provider, __) {
      String imagePath;
      if (provider.showTop && isSelected)
        imagePath = 'jump_to_top.png'.homePath;
      else
        imagePath = isSelected ? state.selectedName[index] : state.imageName[index];

      return Expanded(
        child: InkWell(
          onTap: () {
            if (provider.showTop && isSelected) {
              provider.scrollToTop();
            } else {
              _selectIndex(index);
            }
          },
          child: Container(
            margin: const EdgeInsets.only(top: 8, bottom: 8),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                Container(
                  height: 24,
                  alignment: Alignment.center,
                  child: Image.asset(
                    imagePath,
                    width: isSelected ? 24 : 24,
                    height: isSelected ? 24 : 24,
                  ),
                ),
                Container(
                  padding: const EdgeInsets.only(top: 2),
                  child: Text(
                    (provider.showTop && isSelected) ? '顶部' : titleDesc,
                    style: TextStyle(
                      color: isSelected ? HolidayTheme.theme.tabSelColor : HolidayTheme.theme.tabNorColor,
                      fontSize: 10,
                    ),
                  ),
                ),
              ],
            ),
          ),
        ),
      );
    });
  }

  Widget _buildNormalItem(int index) {
    bool isSelected = index == state.curIndex;
    String imagePath = isSelected ? state.selectedName[index] : state.imageName[index];
    String titleDesc = state.titleArr[index];

    return Expanded(
      child: InkWell(
          onTap: () => _selectIndex(index),
          child: Stack(
            children: [
              Container(
                margin: const EdgeInsets.only(top: 8, bottom: 8),
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Container(
                      height: 24,
                      alignment: Alignment.center,
                      child: Image.asset(
                        imagePath,
                        width: isSelected ? 24 : 24,
                        height: isSelected ? 24 : 24,
                      ),
                    ),
                    Container(
                      padding: const EdgeInsets.only(top: 2),
                      child: Text(
                        titleDesc,
                        style: TextStyle(
                          color: isSelected ? HolidayTheme.theme.tabSelColor : HolidayTheme.theme.tabNorColor,
                          fontSize: 10,
                        ),
                      ),
                    ),
                  ],
                ),
              ),
              if (index == state.titleArr.length - 1)
                Consumer<MineMsgProvider>(
                  builder: (context, model, child) => Positioned(
                    right: 22,
                    top: 24,
                    child: model.buildTipsView(),
                  ),
                ),
            ],
          )),
    );
  }

  @override
  void dispose() {
    super.dispose();
  }
}
